from imports import *
class reptil_all_place():  # 爬虫类: 爬取某个地区的所有景区信息
    def __init__(self, url):
        self.goal = 'https://you.ctrip.com'  # 取网址域名
        self.headers = Headers
        self.url = url  # 景区页面
        self.r = rq.get(url=self.url, headers=self.headers)  # get方式提取html文本
        self.soup = bs(self.r.text, 'html.parser')  # html解析

    def getAll(self):  # 获取所有信息
        '''

        :return: List[List,List,List,List,List,List,List,List]
                     [name, rank, address, comment, score, imagelink, link]
        '''
        return [self.getName(), self.getRank(), self.getAddress(), self.getComment(), self.getScore(),
                self.getImageLink(), self.getLink()]

    def getName(self):  # 获取当前页面的所有景区名称
        NAME = ''.join([str(x) for x in self.soup.select('.rdetailbox > dl > dt > a')])
        self.NAME = re.findall(r'<a[^>]*>([^<]*)</a>', NAME)
        return self.NAME

    def getRank(self):  # 获取当前页面的所有景区排名
        RANK = ''.join([str(x) for x in self.soup.select('.rdetailbox > dl > dt > s')])
        self.RANK = re.findall(r'<s[^>]*>([^<]*)</s>', RANK)
        return self.RANK

    def getAddress(self):  # 获取当前页面的所有景区地址
        ADDRESS = ''.join([str(x) for x in self.soup.select('.rdetailbox > dl > .ellipsis')])
        self.ADDRESS = re.findall(r'\n([^<]*)</dd>', ADDRESS)
        return self.ADDRESS

    def getComment(self):  # 获取当前页面的所有评论
        COMMENTNUM = ''.join([str(x) for x in self.soup.select('.rdetailbox > ul > li > a')])
        self.COMMENTNUM = re.findall(r'[(]([0-9]+)[^<]+', COMMENTNUM)
        return self.COMMENTNUM

    def getScore(self):  # 获取当前页面的所有评分
        SCORE = ''.join([str(x) for x in self.soup.select('.rdetailbox > ul > li > a > strong')])
        self.SCORE = re.findall(r'>([0-9\\.^<]+)<', self.COMMENTNUM)
        return self.SCORE

    def getAllImageLink(self):  # 获取当前页面的所有景区图片链接
        IMGLINK = ''.join([str(x) for x in self.soup.select('.list_mod2 > .leftimg > a > img')])
        self.IMGLINK = re.findall(r'src="([^"]+)"', IMGLINK)
        return self.IMGLINK

    def getLink(self):  # 获取当前页面的所有景区对应网址链接
        LINK = ''.join([str(x) for x in self.soup.select('.list_mod2 > .leftimg > a')])
        self.LINK = [self.goal + x for x in re.findall(r'href="([^"]+)"', LINK)]
        return self.LINK

    def getPagenum(self):  # 获取该地区景区的页数
        PAGENUM = str(self.soup.select('.numpage')[0])
        self.PAGENUM = int(re.findall('>([0-9^<]+)</b>', PAGENUM)[0])
        return self.PAGENUM
    def getNum(self):
        NUM=str(self.soup.select('.ttd_pager')[0])
        self.NUM = int(re.findall('/ ([0-9]+)条', NUM)[0])
        return self.NUM